/*
 * @lc app=leetcode.cn id=530 lang=cpp
 *
 * [530] 二叉搜索树的最小绝对差
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
  int ans, pre;

  int getMinimumDifference(TreeNode* root)
  {
    ans = INT_MAX;
    pre = -1;
    DFS(root);
    return ans;
  }

  void DFS(TreeNode* root)
  {
    if (root) {
      DFS(root->left);
      if (pre != -1) // 第一个数
        ans = min(ans, root->val - pre);
      pre = root->val;
      DFS(root->right);
    }
  }
};
// @lc code=end
